<template>
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-12">
        <b-card header="<i class='fa fa-align-justify'></i> 方案分类">
          <div class="search-box">
            <b-button variant="outline-primary" @click="addSol()">添加解决方案</b-button>
          </div>
          <div class="table-box">
            <table class="table table-striped">
              <thead>
              <tr>
                <th>序号</th>
                <th>标题</th>
                <th>标题详情</th>
                <th>内容详细</th>
                <th>图片</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td colspan="8" v-show="isRfi" style="text-align: center;">没有搜索到匹配的信息!</td>
              </tr>
              <tr  v-for="(item, index) in currentData" :key="index">
                <td>{{  index + 1 + number }}</td>
                <td>{{ item.des }}</td>
                <td>{{ item.designation.substring(0, 10) }} ...</td>
                <td>{{ item.content.substring(0, 15) }} ...</td>
                <!-- <td>{{ item.image1 }}</td> -->
                <td>
                  <img :src="item.image3" alt="">
                </td>
                <td>
                  <span class="badge badge-success" @click="lookSolList(item.sid)">产品</span>
                  <span class="badge badge-success" @click="editSolClassify(item.id)">编辑</span>
                  <span class="badge badge-success" @click="addSolList(item.sid)">添加</span>
                  <span class="badge badge-danger" @click="deleteSolClassify(item.id)">删除</span>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
          <el-pagination
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :current-page.sync="current"
            :page-size="10"
            :total="total">
          </el-pagination>
        </b-card>
      </div>
    </div>

    <div title="Modal title" class="modal-primary" v-model="primaryModal" v-show="primaryModal">
      <span class="modal-open">
        <div class="modal fade show" role="dialog">
          <div class="modal-dialog modal-md">
            <div tabindex="-1" role="document" class="modal-content">
              <header class="modal-header">
                <h5 class="modal-title">删除信息</h5>
                <button type="button" aria-label="Close" class="close" @click="cancelModal()">
                  <span aria-hidden="true">×</span>
                </button>
              </header>
              <div class="modal-body">
                <br>是否删除该条解决方案？<br><br>
              </div>
              <footer class="modal-footer">
                <button type="button" class="btn btn-primary btn-md" @click="cancelModal()">取消</button>
                <button type="button" class="btn btn-danger btn-md" @click="sureDeleteRfi()">确认</button>
              </footer>
            </div>
          </div>
        </div>
        <div class="modal-backdrop fade show"></div>
      </span>
    </div>
  </div>
</template>

<script>
  import { findAllSolution, deleteSolution } from '../../service/api'
  import _ from 'lodash'
  export default {
    name: 'tables',
    data () {
      return {
        current: 1,
        number: 0,
        rfiList: [],
        pageNumber: '',
        companyName: '',
        isRfi: false,
        primaryModal: false,
        delId: ''
      }
    },
    computed: {
      currentData () {
        return this.rfiList[this.current - 1]
      },
      total () {
        return ~~this.pageNumber
      }
    },
    methods: {
      // 关闭浮层
      cancelModal () {
        this.primaryModal = false
      },
      // 查看该条解决方案下的详细列表
      lookSolList (index) {
        this.$router.push({
          path: '/solution/s_list?id=' + index
        })
      },
      // 编辑该条解决方案
      editSolClassify (index) {
        this.$router.push({
          path: '/solution/s_c_editDetail?flag=1&id=' + index
        })
      },
      // 添加一条解决方案
      addSol () {
        this.$router.push({
          path: '/solution/s_c_editDetail?flag=2'
        })
      },
      addSolList (index) {
        this.$router.push({
          path: '/solution/s_l_editDetail?sid=' + index
        })
      },
      // 打开删除浮层
      deleteSolClassify (index) {
        this.delId = index
        this.primaryModal = true
      },
      // 确认删除
      sureDeleteRfi () {
        console.log(this.delId)
        deleteSolution({id: this.delId})
          .then(({ data, err }) => {
            console.log('data: ' + data)
            console.log('err: ' + err)
            if (data.code === 200) {
              alert('删除成功！')
              this.primaryModal = false
              location.reload()
            } else {
              alert('删除失败！')
              this.primaryModal = false
            }
          })
      },
      // 搜索
      searchRif () {
        console.log('companyName: ' + this.companyName)
      },
      handleCurrentChange () {
        console.log('handleCurrentChange: ' + this.current)
        if (this.current === 1) {
          this.number = 0
        } else {
          this.number = (this.current - 1) * 10
        }
      }
    },
    created () {
      // fetch
      findAllSolution()
        .then(({ data, err }) => {
          console.log('err: ' + err)
          console.log('data: ' + data)
          if (data.code === 200) {
            this.pageNumber = data.result.length
            this.rfiList = _.chunk(data.result, 10)
          }
        })
    }
  }
</script>
<style type="text/scss" rel="stylesheet/scss" lang="scss" >
  @import "../../styles/common";
  /*.badge{*/
    /*cursor: pointer;*/
  /*}*/
  /*.search-box{*/
    /*vertical-align: top;*/
    /*margin-bottom: 20px;*/
  /*}*/
  /*.search-box input{*/
    /*vertical-align: top;*/
    /*display: inline-block;*/
    /*width: 250px;*/
  /*}*/
</style>
